layui.use(['layer', 'form', 'ax', 'table', 'admin', 'laydate'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;


    var SysRoleModule = { tableId: "SysRoleModuleTable" };

    var tableResult = table.render({
        elem: '#' + SysRoleModule.tableId,
        url: Feng.ctxPath + '/sysRole/page',
        page: true,
        size: 'sm',
        height: "full-98",
        cellMinWidth: 100,
        toolbar: '#toolbarDemo',
        request: { pageName: 'current', limitName: 'size'},
        // response: { statusName: 'code' ,statusCode: 200 ,msgName: 'message' ,countName: 'total' ,dataName: 'records' },
        parseData: function(res){
            return {
                "code": 0, //解析接口状态
                // "msg": res.message, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.records //解析数据列表
            };
        },
        cols: [[
            {type: 'checkbox'},
            {field: 'roleId', hide: true, title: '主鍵'},
            {field: 'name', title: '角色名称', align: 'center'},
            {field: 'description', title: '角色标识', align: 'center'},
            {toolbar: '#tableBar', title: '操作', align: 'center',  minWidth: 200, fixed: 'right'}
        ]]
    });

    table.on('tool(' + SysRoleModule.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'btnEdit') {
            // SysRoleModule.onEditRole(data);
            admin.putTempData('formOk', false);
            top.layui.admin.open({
                type: 2,
                title: '修改角色',
                content: Feng.ctxPath + '/sysRole/sysRoleEdit?roleId=' + data.roleId,
                end: function () {
                    admin.getTempData('formOk') && table.reload(SysRoleModule.tableId);
                }
            });
        } else if (layEvent === 'btnDelete') {
            // SysRoleModule.onDeleteRole(data);
            ////////删除////////
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/sysRole/remove/"+data.roleId, function (res) {
                    Feng.success("删除成功!");
                    table.reload(SysRoleModule.tableId);
                }, function (res) {
                    Feng.error("删除失败!" + res.message + "!");
                });
                ajax.start();
            };
            Feng.confirm("是否删除角色 " + data.name + "?", operation);
        } else if (layEvent === 'roleAssign') {
            SysRoleModule.roleAssign(data);
        }
    });

    ////////搜索////////
    $('#btnSearch').click(function () {
        SysRoleModule.search();
    });
    SysRoleModule.search = function () {
        var queryData = {};
        queryData['name'] = $("#name").val();
        table.reload(SysRoleModule.tableId, {where: queryData});
    };

    ////////添加////////
    $('#btnAdd').click(function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加角色',
            content: Feng.ctxPath + '/sysRole/sysRoleAdd',
            end: function () {
                admin.getTempData('formOk') && table.reload(SysRoleModule.tableId);
            }
        });
    });

    ////////重置////////
    $('#btnReset').click(function () {
        $("#roleName").val("");
        SysRoleModule.search();
    });

    ////////导出////////
    $('#btnExp').click(function () {
        var checkRows = table.checkStatus(SysRoleModule.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    });







    ////////角色赋权////////
    SysRoleModule.roleAssign = function (data) {
        layer.open({
            type: 2,
            title: '权限配置',
            area: ['300px', '450px'], //宽高
            fix: false,
            maxmin: true,
            content: Feng.ctxPath + '/sysRole/role_assign/' + data.roleId,
            end: function () {
                table.reload(SysRoleModule.tableId);
            }
        });
    };

});
